
<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";
import {axiosInstance} from "@/assets/js/axios-utils.js";
import {useCounterStore} from "@/assets/js/counters.js";

const counterStore = useCounterStore()

const infoForm = ref({})

const login = ()=>{
    if (!infoForm.value.username?.trim()){
        ElMessage.error("用户名不能为空");return;
    }
    if (!infoForm.value.password?.trim()){
        ElMessage.error("密码不能为空");return;
    }
    axiosInstance.post("/account/login",infoForm.value).then(res=>{
        counterStore.isLogin = true
        localStorage.setItem("login",res.data)
        ElMessage.success("登录成功");
    }).catch(err=>{
        ElMessage.error(err.message)
    })
}
</script>

<template>

  <div class="login-box">
      <el-form v-model="infoForm">
          <el-form-item label="用户名" label-width="20%" label-position="left">
              <el-input  placeholder="请输入用户名" clearable  v-model="infoForm.username"/>
          </el-form-item>
          <el-form-item label="密码" label-width="20%" label-position="left">
              <el-input  placeholder="请输入用密码" type="password" clearable  v-model="infoForm.password"/>
          </el-form-item>
          <el-button style="width: 100%;" type="primary" @click="login">登录</el-button>
      </el-form>
  </div>

</template>

<style scoped>
  .login-box{
      width: 400px;
      margin-left: auto;
      margin-right: auto;
      margin-top: 20%;
  }
</style>